//
//  LBGuideController.m
//  LoveBeen
//
//  Created by Alex on 16/12/22.
//  Copyright © 2016年 Alex. All rights reserved.
//

#import "LBGuideController.h"
#import "MainTabBarController.h"
#import "LBGuideCell.h"

@interface LBGuideController ()

@property (nonatomic, strong) NSArray *imageArray;

@end

@implementation LBGuideController

static NSString * const reuseIdentifier = @"Cell";


- (instancetype)init
{
    // 创建流水布局
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
    // 修改大小
    layout.itemSize = kScreenSize;
    // 上下的间距为0
    layout.minimumLineSpacing = 0;
    // 横向滑动
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    return [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI {
    
    // 注册单元格
    [self.collectionView registerClass:[LBGuideCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // 分页
    self.collectionView.pagingEnabled = YES;
    // 取消弹簧效果
    self.collectionView.bounces = NO;
    // 取消滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    //  创建按钮
    UIButton *enterButton = [[UIButton alloc] init];
    
    [enterButton setBackgroundImage:[UIImage imageNamed:@"icon_next"] forState:UIControlStateNormal];
    // 让按钮和图片一样大小
    [enterButton sizeToFit];
    // 计算x
    enterButton.x = 3 * kScreenSize.width + (kScreenSize.width - enterButton.width) * 0.5;
    // 计算y
    enterButton.y = kScreenSize.height - 100;
    
    // 添加监听
    [enterButton addTarget:self action:@selector(enterMain) forControlEvents:UIControlEventTouchUpInside];
    [self.collectionView addSubview:enterButton];
}

// 进入按钮的点击事件
- (void)enterMain
{
    // 切换根控制器
    // 创建tabbarvc
    MainTabBarController* vc = [[MainTabBarController alloc] init];
    // 获取key window更改根控制器
    [UIApplication sharedApplication].keyWindow.rootViewController = vc;
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LBGuideCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    NSArray *array = _imageArray[indexPath.item];
    
    cell.backgroundColor = [UIColor magentaColor];
    // 传图片给cell
    cell.imageArray = array;
    
    return cell;
}

- (NSArray *)imageArray {
    
    if (!_imageArray) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"GuidePlist.plist" ofType:nil];
        
        _imageArray = [NSArray arrayWithContentsOfFile:path];
    }
    
    return _imageArray;
}

@end
